package com.hizt.consumer.controller;

import com.hzit.api.bean.Dept;
import com.hzit.api.client.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/consumer")
public class DeptConsumerController {


    //final String BASE_URL = "http://localhost:8001";

    //ribbon使用微服务的名称，会到eureka服务中自动去查找
    final String BASE_URL = "http://SPRINGCLOUD-DEPT";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/list")
    public List<Dept> list() {

        // GET
        String url = BASE_URL + "/dept/list";

        //HttpClient远程访问

        //SpringBoot提供了RestTemplate模板类
        //对HttpClient的封装，功能更加的强大
        //使用之前，必须先加入到容器中
        List<Dept> deptList = restTemplate.getForObject(url, List.class);
        return deptList;
    }

    @GetMapping("/add")
    public boolean add(Dept dept) {

        //添加 请求方式:POST
        String url = BASE_URL + "/dept/add";
        Boolean post = restTemplate.postForObject(url, dept, boolean.class);
        return post;
    }

    @GetMapping("/get/{deptno}")
    public Dept get(@PathVariable Integer deptno) {

        // GET
        String url = BASE_URL + "/dept/get/" + deptno;

        //HttpClient远程访问

        //SpringBoot提供了RestTemplate模板类
        //对HttpClient的封装，功能更加的强大
        //使用之前，必须先加入到容器中
        Dept dept = restTemplate.getForObject(url, Dept.class);
        return dept;
    }


}
